#!/usr/bin/env python
# -*- coding: utf-8 -*-
from distutils.core import setup

import imp
import os
import sys


########################################################################
# Define package locations, 3to2 flags, and test suite.
########################################################################
PY3_PACKAGES = {'adm':                'adm',
                'adm.tests':          'adm/tests',
                'adm._compatibility': 'adm/_compatibility'}

PY2_PACKAGES = {'adm':                'py2src_no-edit/adm',
                'adm.tests':          'py2src_no-edit/adm/tests',
                'adm._compatibility': 'py2src_no-edit/adm/_compatibility'}

TRANSLATE_FLAGS = [(r'.*',              '--nofix=fullargspec'),
                   (r'.*',              '--nofix=itertools'),
                   (r'.*/__init__.py$', '--nofix=str')]

TEST_SUITE = 'adm.tests'


# Set according to active Python version.
if sys.version_info[0] == 3:
    PACKAGE_DIR = PY3_PACKAGES
else:
    PACKAGE_DIR = PY2_PACKAGES


class PackageFinder(object):
    """Import hook for package_dir locations (instantiate and append to
    sys.meta_path).

    """
    def __init__(self, package_dir):
        self.package_dir = package_dir

    def find_module(self, fullname, path=None):
        if fullname not in self.package_dir:
            return None  # <- EXIT!
        pkgpath = self.package_dir[fullname]
        filehandle, path, description = imp.find_module('', [pkgpath])

        class PackageLoader(object):          # Closes over filehandle,
            def load_module(self, fullname):  # path, and description.
                #sys.stderr.write("Importing '%s' from '%s'\n" % (fullname, path))
                #sys.stderr.flush()
                mod = imp.load_module(fullname, filehandle, path, description)
                if filehandle:
                    filehandle.close()
                mod.__loader__ = self  # For introspection.
                return mod

        return PackageLoader()


########################################################################
# Basic setup.py stuff...
########################################################################
if __name__ == '__main__':
    sys.meta_path.append(PackageFinder(PACKAGE_DIR))  # Import hook.
    import adm

    # Prepare extra key word arguments.
    extras = {}
    #if 'setuptools' in sys.modules:
    #    extras['test_suite'] = TEST_SUITE

    setup(name         = 'adm',
          version      = adm.__version__,
          description  = 'Active data mapping operations for Python.',
          url          = 'http://code.google.com/p/activedatamapping/',
          author       = 'Shawn Brown',
          #author_email = 'http://shawnbrown.com/contact',
          license      = 'Apache 2',
          long_description = '',
          #platforms    = '',
          classifiers  = ['Development Status :: 2 - Pre-Alpha',
                          'License :: OSI Approved :: Apache Software License',
                          'Topic :: Scientific/Engineering :: Information Analysis',
                          'Programming Language :: Python :: 2',
                          'Programming Language :: Python :: 2.4',
                          'Programming Language :: Python :: 2.5',
                          'Programming Language :: Python :: 2.6',
                          'Programming Language :: Python :: 2.7',
                          'Programming Language :: Python :: 3',
                          'Programming Language :: Python :: 3.0',
                          'Programming Language :: Python :: 3.1',
                          'Programming Language :: Python :: 3.2',
                          'Programming Language :: Python :: 3.3'],
          packages     = PACKAGE_DIR.keys(),
          package_dir  = PACKAGE_DIR,
          **extras)

